function numPan(num) {
  return num > 9 ? num : '0' + num
}

function getYearFull(year) {
  var d = new Date(year, 0, 1)
  while (d.getDay() !== 1) d.setDate(d.getDate() + 1)

  var to = new Date(year + 1, 0, 1)
  var i = 1
  const weekList = []; const weekMonth = {}
  
  // eslint-disable-next-line no-unmodified-loop-condition
  for (let from = d; from < to;) {
    weekMonth[from.getMonth() + 1] = weekMonth[from.getMonth() + 1] || 0
    weekMonth[from.getMonth() + 1] += 1
    const value = {
      year: year,
      week: i,
      month: from.getMonth() + 1,
      weekInMonth: weekMonth[from.getMonth() + 1],
      start: `${from.getFullYear()}-${numPan(from.getMonth() + 1)}-${numPan(from.getDate())}`,
      startTime: new Date(`${from.getFullYear()}-${(from.getMonth() + 1)}-${from.getDate()}`).setHours(0, 0, 0, 0)
    }
    from.setDate(from.getDate() + 6)
    if (from < to) {
      value.end = `${from.getFullYear()}-${numPan(from.getMonth() + 1)}-${numPan(from.getDate())}`
      value.endTime = new Date(`${from.getFullYear()}-${(from.getMonth() + 1)}-${from.getDate()}`).setHours(23, 59, 59, 999)
      from.setDate(from.getDate() + 1)
    } else {
      value.end = `${to.getFullYear()}-${(to.getMonth() + 1)}-${to.getDate()}`
      value.endTime = new Date(`${to.getFullYear()}-${(to.getMonth() + 1)}-${to.getDate()}`).setHours(23, 59, 59, 999)
    }

    weekList.push(value)
    i++
  }
  return weekList
}

export default getYearFull
